<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager extends CI_Controller {
	public $client = '';
	public $arrData = '';
	public $marketing_types = '';
	public function __construct() {
		parent::__construct();
		$this->output->set_header("Content-type: text/html; charset=utf-8");
		$this->load->model('Comm');
		$this->load->helper('form');
		$this->load->model('Manager_interface','M');
		$arrAdmin = json_decode($this->Comm->getSession('admin'), true);

		$function = isset($this->uri->segments[2]) ? $this->uri->segments[2] : NULL;
		if(empty($arrAdmin)) {
			if($function != 'login' && $function != 'checkvalidate'  && !empty($function)) {
				$this->load->helper('url');
				redirect('/manager/login', 'location', 302);
			}
		}
		if(!empty($arrAdmin)) {
			$this->admin = $arrAdmin;
			$this->arrData['function'] = $function;
			$this->arrData['power'] = $this->Comm->getList('functions', 'fid, function_name, display, parent, orderid, functions', "fid in("
						.$this->admin['powers'].")", NULL, array('orderBy'=>'parent, orderid'));
		}
		$this->arrData['message'] = '';
	}
	public function index() {
	}
	public function mhomes() {
		$this->arrData['__META'] = $this->Comm->getMeta('管理首页');
		$this->load->view('manager/main', $this->arrData);
	}
	public function top() {
		$this->arrData['admin'] = $this->admin;
		$this->load->view('manager/top', $this->arrData);
	}
	public function menu() {
		$this->load->view('manager/menu', $this->arrData);
	}
	public function welcome() {
		$this->arrData['now'] = date("H");
		$this->arrData['admin'] = $this->admin;
		$this->load->view('manager/welcome', $this->arrData);
	}
	public function checkvalidate($validate = NULL, $return = false) {
		if(empty($validate)) $validate = $this->input->get('validate');
		$validate = strtolower($validate);
		if(empty($validate)){if(!$return) { echo 2; return;} else {return 2;}} ;
		$cookie_validate = strtolower($this->Comm->getCookie('validate'));
		if($validate != $cookie_validate) {if(!$return){ echo 2; return;} else {return 2;}};
	}
	public function login() {
		if($this->input->post('__H') == 'adminLogin' && ($email = $this->input->post('email')) != "" 
			&& ($passwd = $this->input->post('password') != "")) {
			$validate = $this->input->post('validate');
			if($this->checkvalidate($validate, true) != 2) {
				$arrAdmin = $this->M->getAdmin();
				if(!empty($arrAdmin)) {
					$this->Comm->setSession(json_encode($arrAdmin), 'admin');
					$this->load->helper('url');
					redirect('/manager/mhomes', 'location', 302);
				}
			} 
			$this->arrData['message'] = "您输入的密码有误，请重新输入！";	
		}
		$this->load->view('manager/login.php', $this->arrData);
	}
	
	public function logout() { 
		$this->load->helper('url');
		$this->Comm->delSession('admin');
		redirect('/manager/login', 'location', 302);
	}
	
	public function myinfo() {
		$this->arrData['admin'] = $this->admin;
		if($this->input->post('__H') == 'editMyinfo') {
			$arrPost = $this->input->post();
			unset($arrPost['__H']);
			$arrPost['passwd'] = md5(md5($arrPost['passwd']) . $this->config->item('admin_key'));
			if($arrPost['passwd'] != $this->arrData['admin']['passwd'])  {
				$this->arrData['message'] = "您输入的密码有误，请重新输入！";
			} else {
				if($arrPost['re_password'] != '') {
					$arrPost['passwd'] = $arrPost['re_password'];
					$arrPost['passwd'] = md5(md5($arrPost['passwd']) . $this->config->item('admin_key'));
				}
				unset($arrPost['re_password']);
				$this->Comm->update('manager', $arrPost, array('aid'=>$this->arrData['admin']['aid']));
				$arrAdmin = $this->admin;
				$arrAdmin['nick_name'] = $arrPost['nick_name'];
				$this->Comm->setSession(json_encode($arrAdmin), 'admin');
				$this->arrData['message'] = "修改用户信息成功！";
				$this->arrData['admin'] = $arrAdmin;
			}
		}
		$this->load->view('manager/myinfo', $this->arrData);
	}
	
	public function channel() {
		if($this->input->post('__H') != '' && $this->input->post('title') != '') {
			$arrPost = $this->input->post();
			foreach($arrPost as $k => $v) {
				$arrPost[$k] = trim($v);
			}
			if($this->input->post('__H') == 'addChannel') {
				$parent = $this->input->post('parent');
				$arrPost['en_title'] = str_replace(' ', '_', $arrPost['en_title']);
				if($parent != 0) {
					$arrParent = explode('-', $parent);
					$arrPost['parent'] = $arrParent[0];
					$arrPost['depth'] = $arrParent[1] + 1;
					$arrPost['channel'] = $arrParent[2];
					$arrChannelChild = $this->Comm->getChannel($this->M->getChannel(), $arrPost['parent']);
					$orders = $arrChannelChild[count($arrChannelChild) - 1]['orders'];
					$arrPost['orders'] = $orders + 1;
					$this->M->updateChannelOrder("channel ='".$arrPost['channel']."' AND orders > '".$orders."'");		
				}
				unset($arrPost['__H']);
				if($this->Comm->insert('channel', $arrPost)) {
					$insertId = $this->Comm->getInsertId();
					if($insertId > 0 && $parent == '0') {
						$this->Comm->update('channel', array('channel'=>$insertId), array('cid'=>$insertId));			
					}
					if($parent > 0) {
						$this->Comm->set("children", "CONCAT(children, ',$insertId')", false);
						$this->Comm->update('channel', NULL, array('cid'=>$parent));
					}
					$this->arrData['message'] = '类别管理：添加类别成功！';
				}
			}
			if($this->input->post('__H') == 'editChannel') {
				$cid = $arrPost['cid'];
				if($cid > 0) {
					unset($arrPost['__H']);
					unset($arrPost['cid']);
					$this->Comm->update('channel', $arrPost, array('cid'=>$cid));	
				}
			}
			$this->Comm->createChannelFile();
			if($this->input->post('__H') == 'editChannel') {
				echo json_encode(array('success'=>1));
				return;
			}
		}
		
		$this->arrData['channel'] = $this->Comm->getChannel();
		$this->setMenu();
	}
	public function attribute() {
		$cid = $this->input->get('cid');
		$cid = $cid === false ? '' : $cid;
		$arrExtend['orderBy'] = 'cid asc, father_id asc, orders asc, aid asc';
		if($this->input->post('__H') == 'add') {
			$arrPost = $this->input->post();
			$arrPost['en_title'] = str_replace(' ', '_', $arrPost['en_title']);
			if(isset($arrPost['is_father']) && $arrPost['is_father'] == 1) $arrPost['father_id'] = 0;
			unset($arrPost['__H']);
			if(!empty($arrPost['title'])) {
				$arrPost['orders'] = $arrPost['cid'] . '-' . $arrPost['father_id'];
				$this->Comm->insert('attribute', $arrPost);
				if($arrPost['father_id'] == 0) {
					$father_id = $this->Comm->getInsertId();
					$this->Comm->update('attribute', array('father_id'=>$father_id), array('aid'=>$father_id));
				}
				$this->Comm->createTableCache('attribute', NULL, false, $arrExtend);
				$this->arrData['message'] = '加入属性成功！';
			}
			$cid = $arrPost['cid'];
		}
		$this->arrData['filterCount'] = 0;
		if($cid != NULL) $this->arrData['filterCount'] = $this->Comm->getCount('attribute', "cid in(0, $cid) and is_filter = '1'");
		$this->arrData['attribute'] = $this->Comm->getTableCache('attribute', NULL, false, NULL, $arrExtend);
		$this->arrData['attribute_title'] = $this->Comm->tableHash('aid', $this->arrData['attribute'], 'is_father', '1');
		$this->arrData['cid'] = $cid;
		$this->arrData['channel'] = $this->Comm->getChannel();
		$this->setMenu();
	}
	public function property() {
		$cid = $this->input->get('cid');
		$cid = $cid === false ? '' : $cid;
		$arrExtend['orderBy'] = 'cid asc, pid asc';
		if($this->input->post('__H') == 'add') {
			$arrPost = $this->input->post();
			unset($arrPost['__H']);
			if(!empty($arrPost['title'])) {
				$this->Comm->insert('property', $arrPost);
				$this->Comm->createTableCache('property', NULL, false, $arrExtend);
				$this->arrData['message'] = '加入通用多选属性成功！';
			}
			$cid = $arrPost['cid'];
		}
		$property = $this->Comm->getTableCache('property', NULL, false, NULL, $arrExtend);
		$this->arrData['property'] = $this->Comm->tableHash('pid', $property, 'cid', $cid);
		$this->arrData['cid'] = $cid;
		$this->arrData['channel'] = $this->Comm->getChannel();
		$this->setMenu();
	}
	public function hosts() {
		if(trim(($local_path = $this->input->post('local_path'))) != '') {
			preg_match('/^([0-9a-z\/]*)$/i', $local_path, $arrMatch);
			if(!isset($arrMatch[1]) || !isset($arrMatch[0]) || empty($arrMatch)) {
				echo json_encode(array('message' => '本地存储路径验证不通过，请重新填写！'));
				return;
			}
		}
		if($this->input->post('__H') == 'update') {
			$arrPost = $this->input->post();
			$hid = $arrPost['hid'];
			unset($arrPost['hid']);
			unset($arrPost['__H']);
			if(!empty($arrPost['host_name']) && !empty($arrPost['title'])) {
				$this->Comm->update('hosts_ip', $arrPost, array('hid'=>$hid));
				$this->Comm->createTableCache('hosts_ip');
				$this->arrData['message'] = '更新信息成功！';
				echo json_encode(array('message'=>'更新信息成功'));
				return;
			}
		}
		if($this->input->post('__H') == 'add') {
			$arrPost = $this->input->post();
			unset($arrPost['__H']);
			if(!empty($arrPost['host_name']) && !empty($arrPost['title'])) {
				$this->Comm->insert('hosts_ip', $arrPost);
				$this->Comm->createTableCache('hosts_ip');
				$this->arrData['message'] = '加入host成功！';
			}
		}
		if($this->input->post('__H') == 'del') {
			$this->Comm->delete('hosts_ip', array('hid'=>999999));
			$this->arrData['message'] = '删除host成功！';
			$this->Comm->createTableCache('hosts_ip');
		}
		$this->arrData['hosts'] = $this->Comm->getTableCache('hosts_ip');
		$this->setMenu();
	}
	public function setsite() {		
		if($this->input->post('__H') == 'update') {
			$arrPost = $this->input->post();
			unset($arrPost['__H']);
			$this->Comm->update('siteinfo', $arrPost, array('sid'=>1));
			$this->Comm->createTableCache('siteinfo', array('sid'=>1), true);
			$this->arrData['message'] = '更新信息成功！';
		}
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
		$this->setMenu();
	}
	public function mypower() {
		$this->arrData['message'] = '';
		$this->load->view('manager/mypower', $this->arrData);
	}
	
	public function updatecache() {
		if($this->input->post('__H') == 'updatecache') {
			$this->Comm->createTableCache('siteinfo', array('sid'=>1), true);
			$this->Comm->createTableCache('hosts_ip');
			$this->Comm->createChannelFile();
			$this->Comm->createTableCache('marketing');
			$arrExtend['orderBy'] = 'cid asc, father_id asc, orders asc, aid asc';
			$this->Comm->createTableCache('attribute', NULL, false, $arrExtend);
			$arrExtend['orderBy'] = 'cid asc, pid asc';
			$this->Comm->createTableCache('property', NULL, false, $arrExtend);
			$this->arrData['message'] = '更新缓存成功！';
		}
		$this->setMenu();
	}
	public function lang () {
		$this->setMenu();
	}
	public function content() {
		$this->setMenu();
	}
	
	public function auditing() {
		$pass = $this->input->get('pass');
		$passid = $this->input->get('passid');
		if($pass != '' && $passid > 0) {
			if($pass == 'pass') {$this->Comm->update('reading_temporary', array('auditing'=>'2'), array('rtid'=>$passid));}
			if($pass == 'nopass') {$this->Comm->update('reading_temporary', array('auditing'=>'3'), array('rtid'=>$passid));}
			echo json_encode(array('result'=>'success'));
			return;
		}
		$this->arrData['reading'] = $this->Comm->getList('reading_temporary', 'rtid, title, file_title, uid, price, add_date, auditing', 
		                            "auditing = '1'", array(0, 500));
		$this->setMenu();
	}
	public function menber() {
		$this->setMenu();
	}
	public function marketing() {
		$types = $this->marketing_types == '' ? 'ad' : $this->marketing_types;
		$cid = $this->input->get('cid');
		$cid = $cid == '' ? 0 : $cid;
		if($this->input->post('__H') == 'update') {
			$arrPost = $this->input->post();
			unset($arrPost['__H']);
			$cid = $arrPost['cid'];
			if($cid != '') {
		 		unset($arrPost['cid']);
				$this->Comm->update('marketing', $arrPost, array('mid'=>$types.$cid));
				$this->Comm->createTableCache('marketing');
				$this->arrData['message'] = '更新成功！';
			}
		}
		$this->arrData['marketing'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', $types.$cid));
		if(empty($this->arrData['marketing'])) {
			$this->Comm->insert('marketing', array('cid'=>$cid, 'mid'=>$types.$cid));
			$this->Comm->createTableCache('marketing');
			$this->arrData['marketing'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', $types.$cid));
		}
		$this->arrData['channel'] = $this->Comm->getChannel();
		$this->arrData['cid'] = $cid;
		$this->setMenu();
	}

	public function recommend() {
		$this->marketing_types = 'recommend';
		$this->marketing();
	}
	
	public function setMenu() {
		$parent = '';
		$arrSetsiteMenu = '';
		foreach ($this->arrData['power'] as $k => $v) {
			if($v['functions'] == $this->arrData['function']) {
				$parent = $v['parent'];
			}
		}
		foreach ($this->arrData['power'] as $k => $v) {
			if($parent == $v['parent'] && $v['display'] == 1) {
				$arrSetsiteMenu[] = $v;	
			}
		}
		$this->arrData['setsitemenu'] = $arrSetsiteMenu;
		$this->load->view('manager/setsite', $this->arrData);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */